﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MengLin.OOP.DesignPattern.Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
            #region 控制不住多线程
            //Singleton singleton1 = Singleton.CreateInstance1();
            //Singleton singleton2 = Singleton.CreateInstance1();
            //{
            //    for (int i = 0; i < 5; i++)
            //    {
            //        Task.Run(() =>
            //        {
            //            Singleton singleton = Singleton.CreateInstance1();
            //        });
            //    }
            //}
            #endregion

            #region 可以控制住多线程-写法麻烦
            //{
            //    for (int i = 0; i < 5; i++)
            //    {
            //        Task.Run(() =>
            //        {
            //            Singleton singleton = Singleton.CreateInstance2();
            //        });
            //    }
            //}
            #endregion

            #region 可以控制住多线程-利用静态构造函数
            {
                SingletonSecond singleton1 = SingletonSecond.GetInstance();

                SingletonSecond singleton2 = SingletonSecond.GetInstance();

                //for (int i = 0; i < 5; i++)
                //{
                //    Task.Run(() =>
                //    {
                //        SingletonSecond singleton = SingletonSecond.GetInstance();
                //    });
                //}
            }
            #endregion

            #region 可以控制住多线程-利用静态私有全局变量
            {
                SingletonThree singleton1 = SingletonThree.GetInstance();

                SingletonThree singleton2 = SingletonThree.GetInstance();

                //for (int i = 0; i < 5; i++)
                //{
                //    Task.Run(() =>
                //    {
                //        SingletonThree singleton = SingletonThree.GetInstance();
                //    });
                //}
            }
            #endregion

            Console.Read();
        }
    }
}
